package com.huanghuai.retail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huanghuai.retail.entity.PType;
import com.huanghuai.retail.mapper.TypeMapper;
import com.huanghuai.retail.service.TypeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, PType> implements TypeService {

    @Autowired
    private TypeService typeService;

    @Override
    public List<Long> getIdsByName(String name) {

        List<Long> idList = new ArrayList<>();

        LambdaQueryWrapper<PType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(name!=null, PType::getNameTwo, name);
        lambdaQueryWrapper.orderByAsc(PType::getId);

        List<PType> typeList = typeService.list(lambdaQueryWrapper);

        for(int i = 0; i < typeList.size(); i ++)
            idList.add(typeList.get(i).getId());

        return idList;
    }

    /**
     * 根据商品类别name,查询商品类别id
     * 这里商品类别有两类呈包含关系,如饮料,乳制品等
     */
    @Override
    public List<Long> findIdsByName(String typeFirstName,String typeSecondName) {
        List<Long> ids = new ArrayList<>();
        LambdaQueryWrapper<PType> queryWrapper =new LambdaQueryWrapper<>();
        if(typeFirstName==null&&typeSecondName==null){
            return null;
        }
        if (typeFirstName!=null)
        {
            queryWrapper.like(typeFirstName!=null,PType::getNameOne,typeFirstName);
        }
        if(typeSecondName!=null){

            queryWrapper.like(typeSecondName!=null,PType::getNameTwo,typeSecondName);
        }
        List<PType> typeList = typeService.list(queryWrapper);

        for (int i = 0; i < typeList.size(); i++) {
            ids.add(typeList.get(i).getId());
        }
        return ids;
    }
}
